package com.common.utils.log;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author xin.ding
 * @date 2019/12/30 15:46
 */
public class ConsoleLogger implements Serializable {
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private static ConsoleLogger instance;
    private String name;


    public static synchronized ConsoleLogger getInstance() {
        if (null == instance) {
            instance = new ConsoleLogger();
        }
        return instance;
    }

    public void log(final String format, Object... argArray) {
        FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
        Date nowTime = new Date(System.currentTimeMillis());
        String message = String.format("[%d][%s] %s", Thread.currentThread().getId(), dateFormat.format(nowTime), ft.getMessage());
        if (StringUtils.isNotBlank(name)) {
            message = "[" + name + "]" + message;
        }

        System.out.println(message);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}